<?php

namespace App\Http\Controllers\Admin;

use App\Model\Admin;
use Illuminate\Http\Request;

use App\lib\TreeClass;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;


//权限管理器
class AuthController extends CommonController
{

    /*——————————————————————————————   权限    ————————————————————————————————————————*/

    //权限列表
    public function permiss()
    {
        $user = Auth::user();
        //$data = DB::table(config('permission.table_names.role_has_permissions'))->get();
        $permissDatas = Permission::all()->toArray();
        $permissDatas = TreeClass::tree($permissDatas, $title = 'describe', $fieldPri = 'id', $fieldPid = 'pid');
        return view('admin.auth.permiss', compact('permissDatas'));

    }

    //添加权限
    public function permissadd($id, Request $request)
    {
        if ($request->isMethod('post')) {
            $permissname = $request->name;
            $permiss = Permission::create(['name' => $permissname]);
            $permiss->pid = $id;
            $permiss->describe = $request->describe;
            $permiss->save();
            return redirect('/admin/permiss')->with('status', '操作成功');
        }
        return view('admin.auth.permissadd', compact('id'));
    }


    //编辑权限
    public function permissedit($id, Request $request)
    {
        if ($request->isMethod('post')) {
            $permiss = Permission::find($id);
            $permiss->name = $request->name;
            $permiss->describe = $request->describe;
            $permiss->save();
            return redirect('/admin/permiss')->with('status', '操作成功');
        }
        $data = Permission::find($id);
        return view('admin.auth.permissedit', compact('data'));
    }

    //删除权限
    public function permissdel($id, Request $request)
    {
        if ($request->isMethod('post')) {
            $hasSon = Permission::where('pid', $id)->count();
            if ($hasSon > 0) {
                return redirect('/admin/permiss')->with('status', '请删除子权限');
            }
            Permission::destroy($id);
            return redirect('/admin/permiss')->with('status', '操作成功');
        }
    }

    /*——————————————————————————————   角色    ————————————————————————————————————————*/
    //角色
    public function role()
    {
        $roles = Role::paginate(5);
        return view('admin.auth.role', compact('roles'));
    }

    //添加角色
    public function roleadd(Request $request)
    {
        if ($request->isMethod('post')) {
            $rolename = $request->name;
            if (Role::where('name', $rolename)->first() || empty($rolename)) {
                return redirect('/admin/role')->with('status', '角色已存在或不能为空');
            }
            $role = Role::create(['name' => $rolename]);
            $role->describe = $request->describe;
            $role->save();
            return redirect('/admin/role')->with('status', '操作成功');
        }
        return view('admin.auth.roleadd');
    }

    //编辑角色
    public function roleedit($id)
    {
        if (request()->isMethod('post')) {
            $permissions = request()->get('permissions');
            $role = Role::find($id);
            $role->syncPermissions($permissions);
            return redirect('/admin/role')->with('status', '编辑成功');
        }

        //当前角色的权限
        //声明当前角色的权限id集合
        $currentPermissArr = [];
        $currentPermissDatas = DB::table('role_has_permissions')->where('role_id', $id)->get();
        foreach ($currentPermissDatas as $currentPermissData) {
            array_push($currentPermissArr, $currentPermissData->permission_id);
        }
        //获取所有权限
        $allPermissDatas = Permission::all();
        $allPermissDatas = TreeClass::channelLevel($allPermissDatas, $pid = 0, $html = "&nbsp;", $fieldPri = 'id', $fieldPid = 'pid', $level = 1);
        return view('admin.auth.roleedit', compact('allPermissDatas', 'currentPermissArr'));
    }


    //删除角色
    public function roledel($id, Request $request)
    {
        if ($request->isMethod('post')) {
            Role::destroy($id);
            return redirect('/admin/role')->with('status', '操作成功');
        }
    }

    /*——————————————————————————————   角色 -- 用户    ————————————————————————————————————————*/

    public function userrole($id, Request $request)
    {

        if ($request->isMethod('post')) {
            $data = $request->all('permissions');
            $user = Admin::find($id);
            $user->syncRoles($data);
            return redirect('/admin/admin')->with('status', '操作成功');
        }

        //查询所有角色
        $allRoles = Role::all()->toArray();
        //查询该用户所属角色
        //声明当前角色id集合
        $currentRolesArr = [];
        $currentRolesDatas = DB::table('model_has_roles')->where('model_id', $id)->get();
        foreach ($currentRolesDatas as $currentRolesData) {
            array_push($currentRolesArr, $currentRolesData->role_id);
        }
        return view('admin.auth.userrole', compact('allRoles', 'currentRolesArr'));
    }

}
